import { generateSignCode, QrStatus } from "@/api/sign/qr-code";

export const qrCodeMixin = {
  data() {
    return {
      // 二维码
      dialogQrVisible: false,
      QrCodeSrc: undefined,
      key: undefined,
      QrStatus: 0
    }
  },
  methods: {
    async submitAuth() {
      const type = this.authType;
      // 信息确认后提交生成二维码
      const res1 = await generateSignCode(type);
      this.QrCodeSrc = res1.qr;
      this.key = res1.key;
      this.dialogQrVisible = true;

      // 生成二维码后不断请求二维码状态，根据状态进行操作
      while (true) {
        if (this.dialogQrVisible == false) {
          break;
        }
        const k = this.key;
        const res = await QrStatus(this.key, type, this.QrStatus);
        // 如果重新生成了二维码，直接退出循环
        if(this.key != k) {
          break;
        }
        
        this.QrStatus = res.status;
        // 二维码窗口关闭 || 已确认签名 || 签名过期
        if (this.dialogQrVisible == false || this.QrStatus >= 3) {
            setTimeout(() => {
              this.dialogQrVisible = false
              if (this.authType == 1) {
                this.getDesignerInfo();
              } else if (this.authType == 2) {
                this.getStudioInfo();
              } else if(this.authType ==3 ) {
                this.getEnterpriseInfo();
              }
              this.loading = true
            }, 1000);
            break;
        }
      }

    }
  }
}